package houtai.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import houtai.pojo.Comment;
import houtai.pojo.Publish;
import houtai.pojo.UserInfo;
import houtai.util.ListPageUtil;
import houtai.vo.CommentVo;
import houtai.vo.PageResult;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private UserService userService;

    public PageResult queryCommentList(Integer page, Integer pagesize, String sortProp, String sortOrder, String messageID) {
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);

        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(messageID)));
        if (StrUtil.equals(sortOrder, "ascending")) {
            query = query.with(Sort.by(Sort.Order.asc(sortProp)));
        }
        if (StrUtil.equals(sortOrder, "descending")) {
            query = query.with(Sort.by(Sort.Order.desc(sortProp)));
        }

        // 根据动态的id查到动态的所有评论
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        if (CollUtil.isEmpty(commentList)) {
            pageResult.setPages(1);
            pageResult.setCounts(0);
            return pageResult;
        }

        List<CommentVo> commentVoList = new ArrayList<>();
        for (Comment comment : commentList) {
            ObjectId commentId = comment.getId();
            Query query1 = Query.query(Criteria.where("_id").is(commentId));
            if (StrUtil.equals(sortOrder, "ascending")) {
                query1 = query1.with(Sort.by(Sort.Order.asc(sortProp)));
            }
            if (StrUtil.equals(sortOrder, "descending")) {
                query1 = query1.with(Sort.by(Sort.Order.desc(sortProp)));
            }
            // 根据评论的_id查询评论内容
            Publish publish = mongoTemplate.findOne(query1, Publish.class);

            CommentVo commentVo = new CommentVo();
            commentVo.setId(commentId.toHexString());

            UserInfo userInfo = userService.queryUserInfoById(comment.getUserId());

            commentVo.setNickname(userInfo.getNickName());
            commentVo.setUserId(comment.getUserId());
            if (ObjectUtil.isEmpty(publish)) {
                commentVo.setContent(null);
            }
            commentVo.setContent(commentVo.getContent());
            commentVo.setCreateDate(comment.getCreated());

            commentVoList.add(commentVo);
        }
        commentVoList = ListPageUtil.listPaging(commentVoList, page, pagesize);
        pageResult.setItems(commentVoList);
        pageResult.setPages((commentVoList.size() / pagesize) + 1);
        pageResult.setCounts(commentVoList.size());

        return pageResult;
    }
}